<?php
namespace app\spadmin\command;


use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Exception;
use think\Db;
use app\common\model\DingDepartment;
use app\common\model\DingUser;
use dingtalk\DingTalkSdkV1;

class DingTalk extends Command
{
    protected $once;
    protected function configure()
    {
        $this->setName('dingtalk')->setDescription('dingtalk department user update ');
    }

    protected function execute(Input $input, Output $output)
    {
        $output->writeln('dingtalk department user update start');
        $this->once = 0;
        $this->dingDepartment();
        $this->dingUser();
        $output->writeln('end');
    }

    protected function dingDepartment(){
        Db::execute('TRUNCATE TABLE spa_ding_department_user');
        DingDepartment::where('status',1)->where('dept_id','>',1)->update(['status'=>0]);
        $this->dingDepartmentUser();
    }
    protected function dingDepartmentUser($id = 1){
        $dingtalk = DingTalkSdkV1::instance();
        $department = $dingtalk->department($id);
        $this->once++;
        echo $this->once;
        if(intval($this->once)%20 == 0){
            echo $this->once."__";
            sleep(1);
        }
        $userlist = $dingtalk->dempartmentUser($id);
        $this->once++;
        echo $this->once;
        if(intval($this->once)%20 == 0){
            echo $this->once."__";
            sleep(1);
        }
        if(!empty($userlist['userid_list'])){
            $departUser = [];
            foreach ($userlist['userid_list'] as $k => $v) {
                $departUser[] = [
                    'dept_id'=>$id,
                    'userid'=>$v,
                ];
            }
            Db::name('DingDepartmentUser')->insertAll($departUser);
        }
        if(!empty($department)){
            foreach ($department as $key => $value) {
                $dept = DingDepartment::where('dept_id',$value['dept_id'])->find();
                if($dept == null){
                    $dept = new DingDepartment;
                }
                $dept->dept_id = $value['dept_id'];
                $dept->name = $value['name'];
                $dept->parent_id = $value['parent_id'];
                $dept->status =1;
                $dept->save();
                $this->dingDepartmentUser($value['dept_id']);
            }
        }
    }
    protected function dingUser(){
        DingUser::where('status',1)->update(['status'=>0]);
        $dingtalk = DingTalkSdkV1::instance();
        $userid = Db::name('DingDepartmentUser')->group('userid')->column('userid');
        if($userid != null){
            foreach ($userid as $key => $value) {
                $res = $dingtalk->userGet($value);
                $this->once++;
                echo $this->once;
                if(intval($this->once)%20 == 0){
                    echo $this->once."__";
                    sleep(1);
                }
                if(!empty($res)){
                    $user = DingUser::where('userid',$res['userid'])->find();
                    if($user ==  null){
                        $user = new DingUser;
                    }
                    $user->userid = $res['userid'];
                    $user->unionid= $res['unionid']??'';
                    $user->name= $res['name']??'';
                    $user->avatar= $res['avatar']??'';
                    $user->manager_userid= $res['manager_userid']??'';
                    $user->mobile= $res['mobile']??'';
                    $user->job_number= $res['job_number']??'';
                    $user->title= $res['title']??'';
                    $user->work_place= $res['work_place']??'';
                    $user->hired_date= $res['hired_date']??'';
                    $user->active= $res['active']?1:0;
                    $user->real_authed= $res['real_authed']?1:0;
                    $user->senior= $res['senior']?1:0;
                    $user->admin= $res['admin']?1:0;
                    $user->boss= $res['boss']?1:0;
                    $user->status = 1;
                    $user->save();
                }
            }
        }
    }
}